package com.sailrobotics.posterize;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
/**
* Created by asanthan on 7/24/15.
*/
public class ImageEffects {
public static Bitmap doInvert(Bitmap src) {
// create new bitmap with the same settings as source bitmap
Bitmap resultantBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
// color info
int A, R, G, B;
int pixelColor;
// image size
int height = src.getHeight();
int width = src.getWidth();
// scan through every pixel
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// get one pixel
pixelColor = src.getPixel(x, y);
// saving alpha channel
A = Color.alpha(pixelColor);
// inverting byte for each R/G/B channel
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
// set newly-inverted pixel to output image
resultantBitmap.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return resultantBitmap;
}
public static Bitmap doSepia(Bitmap bmpOriginal) {
ColorMatrix sepiaMatrix =new ColorMatrix();
float[] sepMat={0.3930000066757202f, 0.7689999938011169f, 0.1889999955892563f, 0, 0, 0.3490000069141388f, 0.6859999895095825f, 0.1679999977350235f, 0, 0, 0.2720000147819519f, 0.5339999794960022f, 0.1309999972581863f, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1};
sepiaMatrix.set(sepMat);
final ColorMatrixColorFilter colorFilter= new ColorMatrixColorFilter(sepiaMatrix);
Bitmap resultantBitmap = bmpOriginal.copy(Bitmap.Config.ARGB_8888, true);
Paint paint=new Paint();
paint.setColorFilter(colorFilter);
Canvas myCanvas =new Canvas(resultantBitmap);
myCanvas.drawBitmap(resultantBitmap, 0, 0, paint);
return resultantBitmap;
}
public static Bitmap doGreyscale(Bitmap src) {
// constant factors
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
// pixel information
int A, R, G, B;
int pixel;
// get image size
int width = src.getWidth();
int height = src.getHeight();
// scan through every single pixel
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
pixel = src.getPixel(x, y);
// retrieve color of all channels
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// take conversion up to one single value
R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
public static Bitmap doColourBoost(Bitmap src, int type, float percent) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
if(type == 1) {
R = (int)(R * (1 + percent));
if(R > 255) R = 255;
}
else if(type == 2) {
G = (int)(G * (1 + percent));
if(G > 255) G = 255;
}
else if(type == 3) {
B = (int)(B * (1 + percent));
if(B > 255) B = 255;
}
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
}